#!/usr/bin/env python3
"""
DNS管理系统依赖检查脚本
用于验证所有必要的依赖包是否正确安装
"""

import sys
import importlib

def check_import(module_name, display_name=None):
    """检查模块是否可以导入"""
    if display_name is None:
        display_name = module_name
    
    try:
        importlib.import_module(module_name)
        print(f"✅ {display_name} - 导入成功")
        return True
    except ImportError as e:
        print(f"❌ {display_name} - 导入失败: {e}")
        return False

def main():
    """主检查函数"""
    print("🔍 DNS管理系统依赖检查")
    print("=" * 50)
    
    dependencies = [
        ("django", "Django"),
        ("crispy_forms", "Django Crispy Forms"),
        ("crispy_bootstrap5", "Crispy Bootstrap5"),
        ("cryptography", "Cryptography"),
        ("requests", "Requests"),
        ("PIL", "Pillow"),
        ("pymysql", "PyMySQL"),
    ]
    
    # 云服务商SDK检查
    cloud_sdks = [
        ("aliyunsdkcore", "阿里云SDK Core"),
        ("aliyunsdkalidns", "阿里云DNS SDK"),
        ("tencentcloud", "腾讯云SDK"),
    ]
    
    print("\n📦 核心依赖检查:")
    failed_deps = []
    for module, name in dependencies:
        if not check_import(module, name):
            failed_deps.append(name)
    
    print("\n☁️ 云服务商SDK检查:")
    failed_sdks = []
    for module, name in cloud_sdks:
        if not check_import(module, name):
            failed_sdks.append(name)
    
    # 特殊检查：Django版本
    print("\n🐍 版本信息:")
    try:
        import django
        print(f"✅ Django版本: {django.get_version()}")
    except ImportError:
        print("❌ Django未安装")
    
    # 总结
    print("\n" + "=" * 50)
    total_failed = len(failed_deps) + len(failed_sdks)
    
    if total_failed == 0:
        print("🎉 所有依赖检查通过！系统已准备就绪。")
        return True
    else:
        print(f"⚠️  发现 {total_failed} 个问题:")
        
        if failed_deps:
            print(f"   核心依赖问题: {', '.join(failed_deps)}")
            print("   建议执行: pip install -r requirements.txt")
        
        if failed_sdks:
            print(f"   云SDK问题: {', '.join(failed_sdks)}")
            print("   阿里云SDK: pip install aliyun-python-sdk-alidns aliyun-python-sdk-core")
            print("   腾讯云SDK: pip install tencentcloud-sdk-python")
        
        return False

if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)